//
//  YDYEssenceViewController.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 16/7/26.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYEssenceViewController.h"

#import "YDYAllViewController.h"
#import "YDYVideoViewController.h"
#import "YDYVoiceViewController.h"
#import "YDYWordViewController.h"
#import "YDYPhotoViewController.h"
#import "YDYRecommendTagViewController.h"
@interface YDYEssenceViewController ()<UIScrollViewDelegate>
/** 记录tabbaritem，用于判断重复点击 */

@property (nonatomic, weak) UIButton *seletedBtn;
@property (nonatomic, weak) UIView *barBtnLine;

/** scrollview */
@property (nonatomic, weak) UIScrollView *scrollView;
/** titleview*/
@property (nonatomic, weak) UIView *barView;
@end

@implementation YDYEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    ;
    
    //设置导航栏
    [self setupNavigationBarItems];
    //加入控制器
    [self setUpControllers];
    //设置背后的scrollview
    [self setUpScrollView];
     self.scrollView.delegate = self;
    //设置上面的bar
    [self setUpBarView];
    
    
}

#pragma mark -
#pragma mark -setup
-(void)setupNavigationBarItems{
    self.view.backgroundColor = YDYRandomColor;
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    

    self.navigationItem.leftBarButtonItem = [YDYUIBarButtonItemManager ydy_itemWithImgName:@"MainTagSubIcon" selectImgName:@"MainTagSubIconClick" target:self action:@selector(mainTagSubIconDidClick)];

}


-(void)setUpScrollView{
    //新建
    UIScrollView *scrollview = [[UIScrollView alloc]init];
    scrollview.backgroundColor = [UIColor yellowColor];
    scrollview.frame = self.view.bounds;
    [self.view addSubview:scrollview];
    //放入view
    NSInteger count = self.childViewControllers.count;
//    for (int i ; i < count; i++) {
//        UITableView *view = (UITableView *)self.childViewControllers[i].view;
//        view.backgroundColor = YDYRandomColor;
//        view.ydy_x = self.view.ydy_width * i;
//        view.ydy_y = 0;
//        view.ydy_height = self.view.ydy_height;
//        //设置穿透效果的内边距
//        view.contentInset = UIEdgeInsetsMake(64 + 35, 0, 0, 0);
//        //设置滚动条下移
//        view.scrollIndicatorInsets = UIEdgeInsetsMake(64+35, 0, 0, 0);
//        [scrollview addSubview:view];
//    
//    }
    scrollview.contentSize = CGSizeMake(self.view.ydy_width * count, 0);
    scrollview.showsHorizontalScrollIndicator = NO;
    scrollview.showsVerticalScrollIndicator = NO;
    //分页效果
    scrollview.pagingEnabled = YES;
    //穿透效果
    self.automaticallyAdjustsScrollViewInsets = NO;
    //放进属性里面
    self.scrollView = scrollview;
    
    
}
-(void)setUpBarView{
    //标题栏初始化
    UIView *barView = [[UIView alloc]init];
    barView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
    barView.ydy_width = self.view.ydy_width;
    barView.ydy_height = 35;
    barView.ydy_x = 0;
    barView.ydy_y = 64;
    
    //添加标题
    NSArray *titles = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    CGFloat titleW = barView.ydy_width / titles.count;
    CGFloat titleH = barView.ydy_height;
    for (int i ; i < titles.count; i++) {
        //init
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [barView addSubview:btn];
        //data
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        
        //frame
        btn.frame = CGRectMake(i *titleW, 0, titleW, titleH);
    }
    [self.view addSubview:barView];
    
    
    //添加按钮下面的红色划线
    UIView *lineView = [[UIView alloc]init];
    UIButton *btn = barView.subviews.firstObject;
    lineView.backgroundColor = [btn titleColorForState:UIControlStateSelected];

    lineView.ydy_height = 1;
    lineView.ydy_y = barView.ydy_height - lineView.ydy_height;
    self.barBtnLine = lineView;
    [barView addSubview:lineView];
    
    
    //默认选定第一个。注意启动的时候，预先算好宽度,除去动画
    [btn.titleLabel sizeToFit];
//    [self titleBtnClick:barView.subviews[0]];
    btn.selected = YES;
    self.seletedBtn = btn;
    self.barBtnLine.ydy_width = btn.titleLabel.ydy_width;
    self.barBtnLine.ydy_centerX = btn.ydy_centerX;
    self.barView = barView;
    //默认滚动一下
    [self scrollViewDidEndScrollingAnimation:self.scrollView];

    
}
-(void)setUpControllers{
//    @"全部",@"视频",@"声音",@"图片",@"段子"];
    
    YDYAllViewController *allVC = [[YDYAllViewController alloc]init];
    [self addChildViewController:allVC];
    
    YDYVideoViewController *videoVC = [[YDYVideoViewController alloc]init];
    [self addChildViewController:videoVC];
    
    YDYVoiceViewController *voiceVC = [[YDYVoiceViewController alloc]init];
    [self addChildViewController:voiceVC];
    
    YDYPhotoViewController *photoVC = [[YDYPhotoViewController alloc]init];
    [self addChildViewController:photoVC];
    
    YDYWordViewController *wordVC = [[YDYWordViewController alloc]init];
    [self addChildViewController:wordVC];
    
}


-(void)addChildView{
    NSInteger index = self.scrollView.contentOffset.x /self.scrollView.ydy_width;
    UITableViewController *vc = (UITableViewController *)self.childViewControllers[index];
    //addsubview不会重复添加，但是为了frame不重复设置，也可以设置
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = self.scrollView.bounds;
    //设置穿透效果的内边距
    vc.tableView.contentInset = UIEdgeInsetsMake(64 + 35, 0, 0, 0);
    //设置滚动条下移
    vc.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(64+35, 0, 0, 0);
    [self.scrollView addSubview:vc.view];
}

#pragma mark -
#pragma mark -点击方法
-(void)mainTagSubIconDidClick{
    YDYRecommendTagViewController *recommmendVC = [[YDYRecommendTagViewController alloc]init];
    [self.navigationController pushViewController:recommmendVC animated:YES];
    
    
}
-(void)titleBtnClick:(UIButton*)btn{
    //判断是否重复点击
    if (btn == self.seletedBtn) {
        //发通知
        [[NSNotificationCenter defaultCenter] postNotificationName:YDYTitleButtonDidRepeatClickNotification object:nil];
    }
    
    //更换颜色
    self.seletedBtn.selected = NO;
    btn.selected = YES;
    self.seletedBtn = btn;
    //改变按钮下面的红线位置
    [UIView animateWithDuration:0.3 animations:^{
        
        
        self.barBtnLine.ydy_width = btn.titleLabel.ydy_width;
        self.barBtnLine.ydy_centerX = btn.ydy_centerX;
        
    }];
    //滚动scrollview
    CGPoint point = self.scrollView.contentOffset;
    point.x = self.scrollView.ydy_width * btn.tag;
    //不能随意更改offset的y 有可能不是0
    [self.scrollView setContentOffset:point animated:YES];
    
}

#pragma mark -
#pragma mark -scrollview代理方法
//代码滚动调用
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self addChildView];
    
}
//人为滚动调用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self addChildView];
    //把按钮设置为点击状态
    NSInteger index = self.scrollView.contentOffset.x /self.scrollView.ydy_width;
    UIButton *btn = (UIButton *)self.barView.subviews[index];
    [self titleBtnClick:btn];
    
}


@end
